#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include <time.h>
#include <assert.h>
#include <vector>
#include <sys/types.h>
#include <sys/wait.h>
#include "task.hpp"

#define PROCESS_NUM 5
using namespace std;

int waitCommand(int waitFd,bool& quit)
{
    uint32_t command = 0;
    ssize_t s = read(waitFd,&command,sizeof(command));
    assert(s == sizeof(uint32_t));
    if(s == 0) 
    {
        quit = true;
        return -1;
    }
    return command;
}

void sendAndWakeup(pid_t id,int fd,uint32_t command)
{
    write(fd,&command,sizeof(command));
    cout << "main processs : call process " << id << " exectute " << desc[command] << " through " << fd << endl;
}
  
int main()
{
    load();

    //key:子进程的pid    value:与子进程关联的管道文件fd
    vector<pair<pid_t,int>> slots;

    //创建子进程
    for(int i = 0;i < PROCESS_NUM;++i)
    {
        //创建管道
        int pipefd[2] = {0};
        int n = pipe(pipefd);
        assert(n != -1);
        (void)n;

        pid_t id = fork();
        assert(id != -1);

        //子进程读取 
        if(id == 0)
        {
            //child
            close(pipefd[1]);//关闭子进程写端
            while(true)
            {
                bool quit = false;
                int command = waitCommand(pipefd[0],quit);
                if(quit) break;
                //子进程执行命令
                if(command >= 0 && command < handleSize())
                {
                    callbacks[command]();//根据父进程发送的命令，执行对应的函数
                }
                else
                {
                    cout << "非法command" << endl;
                }
            }
            exit(1);
        }

        //father
        close(pipefd[0]);//关闭父进程读端
        slots.push_back(make_pair(id,pipefd[1])); 
    }

    //父进程派发任务
    srand((unsigned int)time(nullptr) ^ getpid() ^ 123456789);//让数据源随机
    while(true)
    {
        //随机版本
        //随机选择一个任务
        int command = rand() % handleSize();
        //随机选择一个子进程
        int choice = rand() % slots.size();
        //派发任务
        sendAndWakeup(slots[choice].first,slots[choice].second,command);
        sleep(1);

        //手动选择版本
        // int select;
        // int command;
        // cout << "############################################" << endl;
        // cout << "#   1.show functional    2.send command     " << endl;
        // cout << "############################################" << endl;
        // cout << "Please select->";
        // cin >> select;
        // if(select == 1) showHandler();
        // else if(select == 2) 
        // {
        //     cout << "Enter your command->";
        //     cin >> command;
        //     //选择进程
        //     int choice = rand() % slots.size();
        //     //把任务给指定进程
        //     sendAndWakeup(slots[choice].first,slots[choice].second,command);
        // }
        // sleep(1);
    }

    //关闭fd，结束所有进程
    for(const auto& slot : slots)
    {
        close(slot.second);
    }

    //回收子进程 
    for(const auto& slot : slots)
    {
        waitpid(slot.first,nullptr,0);
    }
    return 0;
}